/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ywb.edugui.view.subject;

import com.ywb.edugui.controller.ControllerFactory;
import com.ywb.edugui.controller.SubjectController;
import com.ywb.edugui.utils.ExcelUtils;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;
/**
 *
 * @author 10147
 */
public class SubjectAddPanel extends javax.swing.JPanel {
    private static String path = "";
    private static File selectedFile = null;
    private SubjectController subjectController = ControllerFactory.getSubjectController(this);
    /**
     * Creates new form SubjectAddPanel
     */
    public SubjectAddPanel() {
        initComponents();
        jbStep3.setEnabled(false);
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jlStep1 = new javax.swing.JLabel();
        jlStep2 = new javax.swing.JLabel();
        jlStep3 = new javax.swing.JLabel();
        jbStep1 = new javax.swing.JButton();
        jbStep2 = new javax.swing.JButton();
        jbStep3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        taFileList = new javax.swing.JTextArea();

        setLayout(null);

        jlStep1.setText("第一步:");
        add(jlStep1);
        jlStep1.setBounds(39, 59, 63, 25);

        jlStep2.setText("第二步:");
        add(jlStep2);
        jlStep2.setBounds(39, 116, 63, 25);

        jlStep3.setText("第三步:");
        add(jlStep3);
        jlStep3.setBounds(39, 193, 63, 25);

        jbStep1.setText("下载模板");
        jbStep1.setPreferredSize(new java.awt.Dimension(105, 40));
        jbStep1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbStep1ActionPerformed(evt);
            }
        });
        add(jbStep1);
        jbStep1.setBounds(120, 53, 205, 37);

        jbStep2.setText("选择文件");
        jbStep2.setPreferredSize(new java.awt.Dimension(105, 40));
        jbStep2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbStep2ActionPerformed(evt);
            }
        });
        add(jbStep2);
        jbStep2.setBounds(120, 116, 209, 40);

        jbStep3.setText("上传文件");
        jbStep3.setPreferredSize(new java.awt.Dimension(105, 40));
        jbStep3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbStep3ActionPerformed(evt);
            }
        });
        add(jbStep3);
        jbStep3.setBounds(120, 185, 209, 40);

        taFileList.setColumns(20);
        taFileList.setRows(3);
        taFileList.setEditable(false);
        jScrollPane1.setViewportView(taFileList);

        add(jScrollPane1);
        jScrollPane1.setBounds(347, 116, 157, 71);
    }// </editor-fold>

    private void jbStep1ActionPerformed(java.awt.event.ActionEvent evt) {
        JFileChooser chooser = new JFileChooser(new File("D:" + File.separator));
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//        打开文件选择对话框,当返回值为Approve_Option时
        if(chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
            return;
        }

//        获得被选中的文件夹的绝对路径,用于第二步打开文件夹的时候可以直接选择模板文件
        path = chooser.getSelectedFile().getAbsolutePath();
        if(subjectController.downLoadTemplate(path).getSuccess() == true){
            JOptionPane.showMessageDialog(this, "模板下载成功!");
        }
    }

    private void jbStep2ActionPerformed(java.awt.event.ActionEvent evt) {
        //        根据step1被选中的保存文件夹打开文件选择器
        JFileChooser chooser = new JFileChooser(path);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setFileFilter(new FileNameExtensionFilter("only excel", "xlsx", "xls"));
        if(chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION){
            return;
        }
        selectedFile = chooser.getSelectedFile();
        taFileList.append(selectedFile.getName() + '\n');
        jbStep3.setEnabled(true);
    }

    private void jbStep3ActionPerformed(java.awt.event.ActionEvent evt) {
        subjectController.addSubject(selectedFile);

    }
    public void setRegionText(ResourceBundle b){
        jlStep1.setText(b.getString("subject.step1"));
        jlStep2.setText(b.getString("subject.step2"));
        jlStep3.setText(b.getString("subject.step3"));
        jbStep1.setText(b.getString("subject.download"));
        jbStep2.setText(b.getString("subject.select"));
        jbStep3.setText(b.getString("subject.upload"));
    }

    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbStep1;
    private javax.swing.JButton jbStep2;
    private javax.swing.JButton jbStep3;
    private javax.swing.JLabel jlStep1;
    private javax.swing.JLabel jlStep2;
    private javax.swing.JLabel jlStep3;
    private javax.swing.JTextArea taFileList;
    // End of variables declaration
}
